define(
	function (require, exports) {
		yufp.lookup.reg('EXISTS_FLAG,RDB_TYPE');
		// page加载完成后调用ready方法
		exports.ready = function (hashCode, data, cite) {

			yufp.custom.vue({
				el: cite.el,
				data: function () {
					var _this = this;
					return {
						dialogViewFlag: false,
						tabViewFlag: false,
						activeStepNum: 1,
						serverCfgDatas: [],
						currentTypeTabName: "",
						activeTabName: "appServer",
						dataCode: 'EXISTS_FLAG',
						uncheckImg: "themes/use/images/checkbox_unchecked.png",
						appFields: [
							{
								columnCount: 1,
								fields: [
									{
										field: 'appCode',
										label: '系统缩写:',
										placeholder: '应用系统缩写指的是应用系统唯一的英文名称缩写,如: CRM, ODS, ECIF等',
										readonly: true,
										rules: [
											{ required: true, message: '必填项', trigger: 'blur' },
											{ max: 8, message: '输入值过长', trigger: 'blur' }]
									},
									{
										field: 'appName', label: '系统名称:', placeholder: "应用系统名称指的是应用系统的中文名称", readonly: true,
										rules: [
											{ required: true, message: '必填项', trigger: 'blur' },
											{ max: 120, message: '输入值过长', trigger: 'blur' }]
									},
									{
										field: 'serverId', label: '引擎标识:', placeholder: "建议输入C1", readonly: true,
										rules: [
											{ required: true, message: '必填项', trigger: 'blur' },
											{ max: 64, message: '输入值过长', trigger: 'blur' }]
									}
								],
							}],
						loadInitFormData: [],
						databaseServerAllField: [{
							columnCount: 2,
							fields: [{
								field: 'existsFlag',
								label: '默认数据库:',
								type: "select",
								dataCode: "EXISTS_FLAG",
								value: "1",
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' }],
								change: function (val) {
									_this.changeDataType(val);
								},
							},
							{
								field: 'dataType',
								label: '数据库类型:',
								type: "select",
								dataCode: "RDB_TYPE",

								rules: [
									{ required: true, message: '必填项', trigger: 'blur' }]
							},
							{
								field: 'username',
								label: '用户名:',
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' },
									{ max: 64, message: '输入值过长', trigger: 'blur' }]
							},
							{
								field: 'password',
								label: '密码:',
								type: "password",
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' },
									{ max: 64, message: '输入值过长', trigger: 'blur' }]
							},
							{
								field: 'ipAddr',
								label: '服务器地址:',
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' },
									{ max: 32, message: '输入值过长', trigger: 'blur' }]
							},
							{
								field: 'dbPort',
								label: '端口:',
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' },
									{ message: '请输入数字', validator: yufp.validator.number }]
							},
							{
								field: 'dbName',
								label: '实例名:',
								rules: [
									{ required: true, message: '必填项', trigger: 'blur' },
									{ max: 64, message: '输入值过长', trigger: 'blur' }]
							}
							]
						}
						],


						jobTypeList: [
							{
								id: "shell", typeName: '命令行脚本', logImg: 'themes/use/images/shell.png', desc: '可以在操作系统的命令行中直接执行的命令、程序或脚本。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "shellTabRef", serverCfgDatas: [], formRefName: "shellFormRef",
								tipMsg: "tips : 请注意: 默认生成的命令行脚本集群名称为: \"SH\"",
								tabfields: [
									{
										columnCount: 2,
										fields: [
											{
												ctype: "input",
												field: 'agentId',
												label: '代理标识:',
												placeholder: "建议输入A*,各节点代理标识不能重名",
												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											},
											{
												field: 'nodeId',
												label: '节点标识:',
												placeholder: "建议输入N*,各节点标识不能重名",

												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											}
										]
									}
								]
							},
							{
								id: "db", driverType: "db", typeName: '存储过程或SQL语句', logImg: 'themes/use/images/db.png', desc: '存储过程或无输出结果集的SQL语句',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tipMsg: "tips : 请注意: 默认生成的存储过程集群名称为: \"DB\".",
								tableRefName: "dbTabRef", serverCfgDatas: [], formRefName: "dbFormRef",
								tabfields: [
									{
										columnCount: 2,
										fields: [
											{
												ctype: "input",
												field: 'agentId',
												label: '代理标识:',
												placeholder: "建议输入A*,各节点代理标识不能重名",
												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											},
											{
												ctype: "input",
												field: 'nodeId',
												label: '节点标识:',
												placeholder: "建议输入N*,各节点标识不能重名",

												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											}
										]
									}
								]
							},
							{
								id: "ds", typeName: 'DataStage', logImg: 'themes/use/images/ibm.png', desc: '使用IBM InfoSphere DataStage工具开发的作业。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "dsTabRef", serverCfgDatas: [], formRefName: "dsFormRef",
								tipMsg: "tips : 日志过滤规则代码为：I=信息,W=警告,F=致命,R=拒绝,S=开始,T=复位,B=批量,O=其它;请注意: 默认生成的DataStage集群名称为: \"DS\".",
								tabfields: [{
									columnCount: 2,
									fields: [
										{
											field: 'agentId',
											label: '代理标识:',
											placeholder: "建议输入A*,各节点代理标识不能重名",
											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										},
										{
											field: 'nodeId',
											label: '节点标识:',
											placeholder: "建议输入N*,各节点标识不能重名",

											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										}
									]
								}]
							},

							{
								id: "pc", typeName: 'PowerCenter', logImg: 'themes/use/images/powercenter.jpg', desc: '使用Informatica PowerCenter工具开发的作业。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "pcTabRef", serverCfgDatas: [], formRefName: "pcFormRef",
								tipMsg: "tips : 请注意: 默认生成的PowerCenter集群名称为: \"PC\".",
								tabfields: [{
									columnCount: 2,
									fields: [
										{
											field: 'agentId',
											label: '代理标识:',
											placeholder: "建议输入A*,各节点代理标识不能重名",
											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										},
										{
											field: 'nodeId',
											label: '节点标识:',
											placeholder: "建议输入N*,各节点标识不能重名",

											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										}

									]
								}]

							},
							{
								id: "txd", typeName: 'Tuxedo', logImg: 'themes/use/images/tuxedo.png', desc: '使用Tuxedo开发的作业。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "txdTabRef", serverCfgDatas: [], formRefName: "txdFormRef",
								tipMsg: "tips : 请注意: 默认生成的Tuxedo集群名称为: \"TX\".",
								tabfields: [{
									columnCount: 2,
									fields: [
										{
											field: 'agentId',
											label: '代理标识:',
											placeholder: "建议输入A*,各节点标识不能重名",
											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										},
										{
											field: 'nodeId',
											label: '节点标识:',
											placeholder: "建议输入N*,各节点代理标识不能重名",

											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										}
									]
								}]
							},
							{
								id: "javaClass", typeName: 'JavaClass', logImg: 'themes/use/images/JavaClass.jpg', desc: '使用JavaClass开发的作业。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "javaClassTabRef", serverCfgDatas: [], formRefName: "javaClassFormRef",
								tipMsg: "tips : 请注意: 默认生成的JavaClass集群名称为: \"JC\".",
								tabfields: [
									{
										columnCount: 2,
										fields: [
											{
												field: 'agentId',
												label: '代理标识:',
												placeholder: "建议输入A*,各节点代理标识不能重名",
												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											},
											{
												field: 'nodeId',
												label: '节点标识:',
												placeholder: "建议输入N*,各节点标识不能重名",

												rules: [
													{ required: true, message: '必填项', trigger: 'blur' },
													{ max: 64, message: '输入值过长', trigger: 'blur' }]
											}]
									}

								]
							},
							{
								id: "webservice", typeName: 'WebService', logImg: 'themes/use/images/webservice.png', desc: '使用WebSevice 开发的作业。',
								checkImg: 'themes/use/images/checkbox_unchecked.png', checked: false,
								tableRefName: "webserviceTabRef", serverCfgDatas: [], formRefName: "webserviceFormRef",
								tipMsg: "tips : 请注意: 默认生成的WebService集群名称为: \"WS\".",
								tabfields: [{
									columnCount: 2,
									fields: [
										{
											field: 'agentId',
											label: '代理标识:',
											placeholder: "建议输入A*,各节点代理标识不能重名",
											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										},
										{
											field: 'nodeId',
											label: '节点标识:',
											placeholder: "建议输入N*,各节点标识不能重名",

											rules: [
												{ required: true, message: '必填项', trigger: 'blur' },
												{ max: 64, message: '输入值过长', trigger: 'blur' }]
										}

									]
								}]


							},

						],
						firstFormButtons: [
							{
								label: '取消',
								hidden: false,
								click: function () {
									_this.closeAllTab();

								}
							},
							{
								label: '下一步',
								type: 'primary',
								hidden: false,
								click: function () {
									_this.nextTabInfo();
								}
							}

						],
						middleFormButtons: [
							{
								label: '上一步',
								hidden: false,
								click: function () {
									_this.lastTabInfo();

								}
							},
							{
								label: '下一步',
								type: 'primary',
								hidden: false,
								click: function () {
									_this.nextTabInfo();
								}
							}

						],
						endFormButtons: [
							{
								label: '上一步',
								hidden: false,
								click: function () {
									_this.lastTabInfo();

								}
							},
							{
								label: '保存',
								type: 'primary',
								hidden: false,
								click: function () {
									_this.saveGuidle();
								}
							}
						]
					}
				},

				watch: {
					currentTypeTabName: function (val) {
						this.loadInitData(val);
					}
				},

				methods: {


					addServerCfg: function (item) {
						var newObject = {};
						newObject.serverType = "";
						newObject.serverId = "";
						newObject.location = "";

						newObject.remark = "";
						newObject.srvPort = "";

						if (item == null || item.serverCfgDatas == null) {
							newObject.deployDesc = "中央监控";
							this.serverCfgDatas.push(newObject);
						} else {
							newObject.deployDesc = "";
							item.serverCfgDatas.push(newObject);
						}

					},
					deleteServerCfg: function (item) {

						var _this = this;
						if (item == null || item.tableRefName == null) {
							let selectDatas = _this.$refs.servertab.selections;
							for (let jdx = _this.serverCfgDatas.length - 1; jdx >= 0; jdx--) {
								for (let idx = 0; selectDatas.length > idx; idx++) {
									if (_this.serverCfgDatas[jdx] == selectDatas[idx]) {
										this.serverCfgDatas.splice(jdx, 1);
										break;
									}
								}

							}
						} else {

							let tabSelectDatas = _this.$refs[item.tableRefName][0].selections;

							for (let jdx = item.serverCfgDatas.length - 1; jdx >= 0; jdx--) {
								for (let idx = 0; tabSelectDatas.length > idx; idx++) {
									if (item.serverCfgDatas[jdx] == tabSelectDatas[idx]) {
										item.serverCfgDatas.splice(jdx, 1);
										break;
									}
								}

							}

						}

					},

					closeAllTab: function () {
						var _this = this;
						_this.dialogViewFlag = false;
						_this.$nextTick(function () {
							yufp.frame.removeTab(cite.rootId.substr(7))

						});

					},


					lastTabInfo: function () {
						var _this = this;
						if (_this.activeTabName === 'dataBaseSever') {
							_this.activeTabName = 'appServer';
						} else if (_this.activeTabName === 'jobServer') {
							_this.activeTabName = 'dataBaseSever';
						} else if (_this.activeTabName === 'execServer') {
							_this.activeTabName = 'jobServer';
						}
						_this.activeStepNum--;
					},

					loadInitData: function (key) {
						var _this = this;
						_this.$nextTick(function () {
							if (_this.loadInitFormData.indexOf(key) < 0) {
								var obj = _this.getFormDataMap(key);
								if (obj != null && obj.defaultMap != null) {
									_this.$refs[obj.formRefName][0].formdata = obj.defaultMap.formDataMap;
									_this.loadInitFormData.push(key);
								}

							}


						})
					},


					getFormDataMap: function (key) {
						for (let i = 0; i < this.jobTypeList.length; i++) {
							if (this.jobTypeList[i].id === key) {
								return this.jobTypeList[i];
							}
						}
						return null
					},




					nextTabInfo: function () {
						var _this = this;
						console.log(_this.$refs.appServerForm.formModel)
						if (_this.activeTabName === 'appServer') {
							if (!_this.checkAppServer()) {
								return;
							} else {
								_this.activeTabName = 'dataBaseSever';
							}

						} else if (_this.activeTabName === 'dataBaseSever') {

							if (!_this.checkRdbServer()) {
								return;
							} else {
								_this.activeTabName = 'jobServer';
							}

						} else if (_this.activeTabName === 'jobServer') {
							if (!_this.checkJobTypeSelect()) {
								return;
							}


							_this.activeTabName = 'execServer';

							_this.loadInitData(_this.currentTypeTabName);

							/* for(let idx=0;idx<_this.jobTypeList.length;idx++){
									 if(_this.jobTypeList[idx].checked){
											 _this.loadInitData(_this.jobTypeList[idx].id);
											 return ;
									 }
							 }*/



						}
						_this.activeStepNum++;
					},

					checkJobTypeSelect: function () {
						var _this = this;
						for (let idx = 0; idx < _this.jobTypeList.length; idx++) {
							if (_this.jobTypeList[idx].checked) {
								_this.currentTypeTabName = _this.jobTypeList[idx].id;
								return true;
							}
						}
						_this.$message({ message: '请至少选择一个调度类型.', type: 'warning', duration: 6000 });
						return false;


					},

					checkRdbServer: function () {
						var _this = this;
						let validateResult = false;
						_this.$refs.rdbServerForm.validate(function (valid) {
							validateResult = valid;
						});

						if (validateResult) {
							return validateResult;
						} else {
							if (_this.$refs.rdbServerForm.formModel.existsFlag === null ||
								_this.$refs.rdbServerForm.formModel.existsFlag === '' ||
								_this.$refs.rdbServerForm.formModel.existsFlag === '1') {
								return false;
							} else {
								return true;
							}
						}
					},

					checkAppServer: function () {
						var _this = this;
						let validateResult = false;
						_this.$refs.appServerForm.validate(function (valid) {
							validateResult = valid;
						});

						if (validateResult) {
							if (_this.serverCfgDatas.length == 0) {
								_this.$message({ message: '至少填写一个合法的服务器地址', type: 'warning', duration: 6000 });
								validateResult = false;
							} else {
								for (let idx = 0; idx < _this.serverCfgDatas.length; idx++) {
									if (_this.serverCfgDatas[idx].location == null ||
										_this.serverCfgDatas[idx].location == '') {
										_this.$message({ message: '列表中的服务器地址不能为空', type: 'warning', duration: 6000 });
										validateResult = false;
									}
								}
							}
						}

						return validateResult;

					},


					loadDefaultDriver: function () {
						var _this = this;
						yufp.service.request({
							method: 'GET',
							url: "api/use/plats/driver/queryDefaultDrivers",
							data: {},
							async: false,
							callback: function (code, message, response) {
								if (response.code == 0) {
									isOk = false;

									for (let j = 0; j < response.data.length; j++) {
										for (let m = 0; m < _this.jobTypeList.length; m++) {
											if (response.data[j].key === _this.jobTypeList[m].typeName) {
												_this.jobTypeList[m].defaultMap = response.data[j].formData;
												_this.jobTypeList[m].tabfields[0].fields.splice(2, _this.jobTypeList[m].tabfields.length - 2);
												for (let n = 0; n < response.data[j].datas.length; n++) {
													_this.jobTypeList[m].tabfields[0].fields.push(response.data[j].datas[n]);


													// var field = response.data[j].datas[n].field;
													/*   if(response.data[j].datas[n].ctype==='select') {
																 _this.jobTypeList[m].tabfields.push({
																		 type: response.data[j].datas[n].ctype,
																		 field: field,
																		 multiple: response.data[j].datas[n].multiple,
																		 label:  response.data[j].datas[n].label,
																		 rules:  response.data[j].datas[n].rules,
																		 value:  response.data[j].formData[field],
																		 options: response.data[j].datas[n].options,
 
																 })
														 }else{
															_this.jobTypeList[m].tabfields.push({
																	type: response.data[j].datas[n].ctype,
																 field: field,
																 label:  response.data[j].datas[n].label,
																 rules:  response.data[j].datas[n].rules,
																 value:  response.data[j].formData[field]
														 })
														 }*/
												}

											}
										}

									}

								}

							}
						});
					},


					changeDataType: function (val) {
						var _this = this;
						for (var i = 1; i <= 6; i++) {
							_this.$set(_this.databaseServerAllField[0].fields[i], 'hidden', val === '0');

						}

					},

					jobTypeClick: function (jobType) {
						jobType.checked = !jobType.checked
						if (jobType.checked) {
							jobType.checkImg = "themes/use/images/checkbox_checked.png";
						} else {
							jobType.checkImg = "themes/use/images/checkbox_unchecked.png";
						}
						this.tabViewFlag = jobType.checked;

					},

					saveGuidle: function () {
						var _this = this;
						var selectedJobTypeList = [];
						var agentIdList = [];
						var nodeIdList = [];
						for (var idx = 0; idx < _this.jobTypeList.length; idx++) {
							if (_this.jobTypeList[idx].checked) {
								var result = _this.checkSelectTypeData(_this.jobTypeList[idx]);
								if (agentIdList.includes(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.agentId)
									|| nodeIdList.includes(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.agentId)) {
									_this.$message({ message: "代理标识[" + _this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.agentId + "]重复或与节点标识重复，请检查" });
									return;
								} else {
									agentIdList.push(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.agentId);
								}

								if (nodeIdList.includes(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.nodeId)
									|| agentIdList.includes(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.nodeId)) {
									_this.$message({ message: "节点标识[" + _this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.nodeId + "]重复或与代理标识重复，请检查" });
									return;
								} else {
									nodeIdList.push(_this.$refs[_this.jobTypeList[idx].formRefName][0].formdata.nodeId);
								}



								if (!result) {
									return;
								} else {

									if (_this.jobTypeList[idx].tabfields.length > 0) {
										var jobTypeObject = _this.$refs[_this.jobTypeList[idx].formRefName][0].formdata
										jobTypeObject.serverCfgDatas = _this.jobTypeList[idx].serverCfgDatas;
										jobTypeObject.type = _this.jobTypeList[idx].id;
										selectedJobTypeList.push({ "id": jobTypeObject.type, "values": jobTypeObject });
									} else {
										var objectVar = {};
										objectVar.type = _this.jobTypeList[idx].id;
										objectVar.serverCfgDatas = _this.jobTypeList[idx].serverCfgDatas;
										selectedJobTypeList.push({ "id": objectVar.type, "values": objectVar });

									}

								}
							}
						}
						let saveData = {};
						var appServerFormModel = _this.$refs.appServerForm.formModel;
						appServerFormModel.serverCfgDatas = _this.serverCfgDatas;
						saveData.appServerForm = appServerFormModel;
						saveData.rdbServerFormModel = _this.$refs.rdbServerForm.formModel;
						saveData.execServers = selectedJobTypeList;

						yufp.service.request({
							url: 'api/use/plats/guideschedule/saveGuideSchedule',
							method: 'POST',
							data: saveData,
							callback: function (code, message, response) {
								if (response.code == 0) {
									_this.$message('操作成功');
									_this.closeAllTab();
								} else {
									_this.$message(response.message);
								}
							}
						});
					},
					checkSelectTypeData: function (jobType) {
						const _this = this;
						let validateResult = true;
						if (jobType.tabfields.length > 0) {
							_this.$refs[jobType.formRefName][0].validate(function (valid) {
								validateResult = valid;
							});

						}

						if (validateResult) {
							if (jobType.serverCfgDatas.length == 0) {
								_this.$message({ message: '至少填写一个合法的服务器地址', type: 'warning', duration: 6000 });
								validateResult = false;
							} else {
								for (var idx = 0; idx < jobType.serverCfgDatas.length; idx++) {
									if (jobType.serverCfgDatas[idx].location == null ||
										jobType.serverCfgDatas[idx].location == '') {
										_this.$message({ message: '列表中的服务器地址不能为空', type: 'warning', duration: 6000 });
										validateResult = false;
									}
								}
							}
						}

						if (!validateResult) {
							_this.currentTypeTabName = jobType.id;
							return validateResult;
						}

						return validateResult;


					}
				},




				mounted: function () {
					var _this = this;
					_this.loadDefaultDriver();
					_this.$nextTick(function () {
						_this.dialogViewFlag = true;
					});
					setTimeout(function () {
						_this.appFields[0].fields.forEach(function (item) {
							item.readonly = false;
						})
					}, 1000)
				}
			});
		};

		// 消息处理
		exports.onmessage = function (type, message) {

		};

		// page销毁时触发destroy方法
		exports.destroy = function (id, cite) {

		};
	});